import Vue from 'vue';
import VueRouter from 'vue-router';
import GoodList from '../components/good/GoodList.vue';
import Seckill from '../components/good/Seckill.vue';
import OrderList from '../components/order/OrderList.vue';
import LoginBox from '../components/login/LoginBox.vue';
import login from '../store/modules/login';

Vue.use(VueRouter);

const router = new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/goods',  
      component: GoodList
    },
    {
      path: '/seckill/:id',  
      component: Seckill,
      props: true
    },    
    {
        path: '/orders',  
        component: OrderList
    },
    {
      path: '/login',
      component: LoginBox,
      beforeEnter: (to, from, next) => {
        if (login.state.user) next('/goods');
        else next();
      }
    },
    {
      path: '/',
      redirect: '/login'
    }
  ]
});

router.beforeEach((to, from, next) => {
    console.log('路由beforeEach' + login.state.user);
    if (!login.state.user && to.path !== '/login') next('/login');
    else next();
});

// router.beforeEach((to, from, next) => {
//   const token = localStorage.getItem("token");
//   if (!token && to.path !== '/login') next('/login');
//   else next();
// });

export default router;
